/* Appendix Table 3, Column 6 */



#delimit ;
clear all;

local outfile "GWgapr10_pfpool_anz2d_TLsur_Gallen_nonneg";
set more off;


di _n "$S_DATE $S_TIME";











********************************************************************************;
* Setting up locals for regs;

* third order polynomial in K and investment;

local invK_polyn "inv1 inv2 inv3 cap1 cap2 cap3 inv1_cap1 inv2_cap1 inv1_cap2";

local labels `"
	phi_f:_cons "Phi female"
	phi_lt25:_cons "Phi age less than 25 yrs"
	phi_40to54:_cons "Phi sge 40 to 54"
	phi_55p:_cons "Phi age 55 plus"
	phi_pt:_cons "Phi part time (<1 FTE)"
	delta:_cons "Delta (exp on av FTEs or hours)"
	
	lnL:_cons "Labour input (ln)"
	xb_multiplant:_cons "Multiplant"
	xb_WPc_WP_cat_1:_cons "1 working proprietor"
	xb_WPc_WP_cat_2:_cons "2-5 working proprietors"
	xb_WPc_WP_cat_3:_cons "6-10 working proprietors"
	xb_WPc_WP_cat_4:_cons ">10 working proprietors"
	
	xb_s_fthc_25to39:_cons "Share of full time employees aged 25 to 39"
	xb_s_pthc_25to39:_cons "Share of part time employees aged 25 to 39"
	xb_s_fthc_40to54:_cons "Share of full time employees aged 40 to 54"
	xb_s_pthc_40to54:_cons "Share of part time employees aged 40 to 54"
	xb_s_fthc_55p:_cons "Share of full time employees aged 55+"
	xb_s_pthc_55p:_cons "Share of full time employees aged 55+"
	
	xb_s_fthc_ten1:_cons "Share of full time employees in 2nd year at pent"
	xb_s_pthc_ten1:_cons "Share of part time employees in 2nd year at pent"
	xb_s_fthc_ten2:_cons "Share of full time employees in 3rd+ year at pent"
	xb_s_pthc_ten2:_cons "Share of part time employees in 3rd+ year at pent"
	
	xb_sec_govt_prod_ent:_cons "Sector: Government producer enterprise"
	xb_sec_fin_inter:_cons "Sector: Financial intermediary"
	xb_avg_age:_cons "Average employee age (/100)"
	xb_female:_cons "Female"
	"';

	
local order "phi_f_pf:_cons phi_lt25_pf:_cons phi_40to54_pf:_cons phi_55p_pf:_cons

	phi_f_wb:_cons phi_lt25_wb:_cons phi_40to54_wb:_cons phi_55p_wb:_cons
	";
	
/* use this version;
local indicators `"
	"Year fixed effects (pf) = xb_pf_Yyear_*:_cons"
	"Year fixed effects (wb) = xb_wb_Yyear_*:_cons"
	"2-digit ANZSIC06 industry fixed effects (pf) = xb_pf_INind*:_cons"	
	"2-digit ANZSIC06 industry fixed effects (wb) = xb_wb_INind*:_cons"	
	"'; 
*/

* this version is just for fake data;	
local indicators `"
	"Year fixed effects (pf) = xb_pf_Yyear_*:_cons"
	"Year fixed effects (wb) = xb_wb_Yyear_*:_cons"
	"'; 
	
	


******************************;
* Specifying the labour input;	

* head count*(average fte)^delta;

local L_fte_del_pf = "(
	  {phi_lt25_pf=0.5}*(L_hc_m_lt25  *(av_fte_m_lt25)^{delta_pf=1}+{phi_f_pf=0.9}*L_hc_f_lt25  *(av_fte_f_lt25)^{delta_pf})
	+ 					(L_hc_m_25to39*(av_fte_m_25to39)^{delta_pf}+{phi_f_pf}    *L_hc_f_25to39*(av_fte_f_25to39)^{delta_pf})
	+ {phi_40to54_pf=1}*(L_hc_m_40to54*(av_fte_m_40to54)^{delta_pf}+{phi_f_pf}    *L_hc_f_40to54*(av_fte_f_40to54)^{delta_pf})
	+ {phi_55p_pf=1}   *(L_hc_m_55p*(av_fte_m_55p)^{delta_pf}     + {phi_f_pf}    *L_hc_f_55p   *(av_fte_f_55p)^{delta_pf})
	)";
	
local L_fte_del_wb = "(
	{phi_lt25_wb=0.5}  *(L_hc_m_lt25  *(av_fte_m_lt25)^{delta_wb=1}+ {phi_f_wb=0.9}*L_hc_f_lt25*(av_fte_f_lt25)^{delta_wb})
	+ 					(L_hc_m_25to39*(av_fte_m_25to39)^{delta_wb}+ {phi_f_wb}*L_hc_f_25to39  *(av_fte_f_25to39)^{delta_wb})
	+ {phi_40to54_wb=1}*(L_hc_m_40to54*(av_fte_m_40to54)^{delta_wb}+ {phi_f_wb}*L_hc_f_40to54  *(av_fte_f_40to54)^{delta_wb})
	+ {phi_55p_wb=1}   *(L_hc_m_55p   *(av_fte_m_55p)^{delta_wb}   + {phi_f_wb}*L_hc_f_55p     *(av_fte_f_55p)^{delta_wb})
	)";


local vars_fte_del "L_hc_m_lt25 L_hc_f_lt25 av_fte_m_lt25 av_fte_f_lt25
	L_hc_m_25to39 L_hc_f_25to39 av_fte_m_25to39 av_fte_f_25to39
	L_hc_m_40to54 L_hc_f_40to54 av_fte_m_40to54 av_fte_f_40to54
	L_hc_m_55p L_hc_f_55p av_fte_m_55p av_fte_f_55p";

local L_fte_dell "Sum of head count*(average FTEs)^{delta} for all labour types";



	
	
	
	
********************************************************************************;
* Running regressions;

* using obs with hc of 5+ in consisten restricted sample;

use if Q_rest==1 using GWgap_pr_firm_v4, clear;

capture drop *ten*;
capture drop INpf_*;
gen con = 1;
label var con "Constant";


* merging in investment;

merge 1:1 pent year using GWgap_invest_v3b, keep(match) nogen keepus(inv1 inv2 inv3 cap1 cap2 cap3 inv1_cap1 inv2_cap1 inv1_cap2);
keep if inv1_cap1<.;
drop if inv1<0;


xi i.ind2, pre(IN);

	
unab extra_ctls: multiplant WPc_* Yyear* INind* `invK_polyn';

compress;

capture erase "`outfile'.txt";
file open out using "`outfile'.txt", write;
file write out "The pf and wb regressions are translog form where an observation is
a pent in a year and the labour input is a linear combination of male and female labour
inputs. Regressions control for a third order polynomial in capital and investment
to deal with potential endogeneity." _n;

file write out "Regressions are weighted by employee head count or total FTEs." _n _n;
file close out;






foreach lab in fte_del {; * looping over way labour input is measured;

	file open out using "`outfile'.txt", write append;

	eststo clear;
	local outlist `""';

	if "`lab'"=="hc" local wgtvar L_hc_t;
	if "`lab'"=="fte" | "`lab'"=="ftpt" | "`lab'"=="fte_del" | "`lab'"=="hrs" | "`lab'"=="hrs_del"
		local wgtvar L_fte_t;


	local outlist `" `outlist' `lab'`fn' "';

		


	di as input _n _n "$S_DATE $S_TIME: Running reg for lab=`lab'";
						
				
	* which labour measure to use;
	
	di as input "L_pf = `L_`lab'_pf'" _n "`L_`lab'l'";
	file write out "Labour specification: `L_`lab'l'";
	file close out;
		
	preserve;
	
	
	* keeping only the subset of controls that vary;
	local extra_ctls2 "";
	foreach var in `extra_ctls' {;
		qui sum `var';
		if r(sd)>0 & r(sd)<. local extra_ctls2 "`extra_ctls2' `var'";
	};
	
	* listing industry FE in extra_ctls2;
	
	local indvars "";
	local nonindvars "";
	foreach var in `extra_ctls2' {;
		if strpos("`var'","INind")==1 local indvars "`indvars' `var'";
		if strpos("`var'","INind")!=1 local nonindvars "`nonindvars' `var'";
	};
	
	* omitting the first industry FE;
	
	local ind1: word 1 of `indvars';
	local indvars: list indvars - ind1;
	local extra_ctls2: list nonindvars | indvars;
	


	nlsur (lngo = {lnL_pf=1}*ln(`L_`lab'_pf')	
			+ {lnLsq_pf=1}*ln(`L_`lab'_pf')*ln(`L_`lab'_pf')/100
			+ {lnL_lnK_pf=1}*ln(`L_`lab'_pf')*lnK/100
			+ {lnL_lnM_pf=1}*ln(`L_`lab'_pf')*lnM/100
		
			+ {lnK_pf=1}*lnK
			+ {lnKsq_pf=1}*lnKsq
			+ {lnK_lnM_pf=1}*lnK_lnM
			
			+ {lnM_pf=1}*lnM
			+ {lnMsq_pf=1}*lnMsq
			+ {xb_pf: `extra_ctls2' con})
		(lnWB = {lnL_wb=1}*ln(`L_`lab'_wb')	
			+ {lnLsq_wb=1}*ln(`L_`lab'_wb')*ln(`L_`lab'_wb')/100
			+ {lnL_lnK_wb=1}*ln(`L_`lab'_wb')*lnK/100
			+ {lnL_lnM_wb=1}*ln(`L_`lab'_wb')*lnM/100
		
			+ {lnK_wb=1}*lnK
			+ {lnKsq_wb=1}*lnKsq
			+ {lnK_lnM_wb=1}*lnK_lnM
			
			+ {lnM_wb=1}*lnM
			+ {lnMsq_wb=1}*lnMsq
			+ {xb_wb: `extra_ctls2' con})
		[aweight = `wgtvar']
		, cluster(pent);

		
	di as input "Command just run: " e(cmdline);

	local command = e(cmdline);
	file open out using "`outfile'.txt", write append;

	file write out _n "Command `lab':" _n "`command'" _n _n;
	file close out;

	/* use next line to conf observation count;
	rndobs N;
	*/
	

	* Saving the difference between phi in pf and wb equations;
	
	local addsc "";
	local addst "";
	local addn "";

	foreach type in f lt25 40to54 55p {;
	
		local phi_`type'_diff_c = 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		
		nlcom 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		matrix V = r(V);
		local phi_`type'_diff_se = V[1,1]^0.5;
		
		local addsc "`addsc' phi_`type'_diff_c `phi_`type'_diff_c' phi_`type'_diff_se `phi_`type'_diff_se'";
		local addst "`addst' phi_`type'_diff_c phi_`type'_diff_se";
		local addn `"`addn' "1 - phi_`type'_wb/phi_`type'_pf" "1 - phi_`type'_wb/phi_`type'_pf se" "';
	};
	

	eststo `lab', addscalars(`addsc');
	
			
	restore;
	

	local addstats "`addst'";
	local addfmts "%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f";
	local addnames `" `addn' "';

	estout `outlist' using "`outfile'.txt", append
		c(b(star fmt(%9.3f)) se(par))
		style(tab)
		legend label collabels(, none)
		dropped ("dropped")
		eqlabels(`eqlab', merge) 
		varl(`labels')
		order(`order')
		indicate(`indicators')
		mlabels(`outlist')
		stats(r2 N converge `addstats', fmt(%9.3f %9.0gc %3.0f `addfmts') 
			labels("R-squared" Observations Converged `addnames'))
		varwidth(50) modelwidth(10) delimiter("") wrap
		prehead("`=char(13)'`=char(13)'
			`=char(13)'$S_TIME $S_DATE 
			`=char(13)'******************************************************"); 
};


